package school.valon.rest.services;

import java.util.List;

import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;





import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import school.valon.business.UserBusiness;
import school.valon.constantes.EnumStaffFunction;
import school.valon.dto.StaffAttendanceDto;
import school.valon.dto.StaffDto;
import school.valon.dto.StudentDto;
import school.valon.dto.UserDto;

@Path("/users")
@PermitAll
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public class UserService {
	@Autowired()
	@Qualifier("userBusiness")
	private UserBusiness userBusiness;

	@POST
	@RolesAllowed("staff")
	@Produces(MediaType.APPLICATION_JSON)
	public Response registerUser(UserDto userDto) throws Exception {
		userDto = userBusiness.registerUser(userDto);
		return Response.status(Response.Status.CREATED).entity(userDto).build();
	}

	@PUT
	@Path("{userId}")
	@RolesAllowed("staff")
	public Response updateUser(@PathParam("userId") String userId, UserDto userDto) throws Exception {
		userDto.setUserId(Integer.valueOf(userId));
		boolean result = userBusiness.updateUser(userDto);
		return Response.ok("User "  + (result ? "" : "not ") +"updated").build();
	}

	@DELETE
	@Path("{userId}")
	@RolesAllowed("staff")
	public Response deleteUser(@PathParam("userId") String userId) throws Exception {
		userBusiness.deleteUser(Integer.valueOf(userId));
		return Response.ok("User deleted").build();
	}

	@PUT
	@Path("/parents/{parentId}/students")
	@RolesAllowed({"parent"})
	public Response linkStudents(@PathParam("parentId") String parentId,
			List<StudentDto> studentDtoList) throws Exception {
		userBusiness.linkParentToStudents(Integer.valueOf(parentId), studentDtoList);
		return Response.ok("Parent linked to the new students").build();
	}

	@PUT
	@Path("/teachers/{staffId}/roles")
	@RolesAllowed({"advisor"})
	public Response updateTeacherRoles(@PathParam("staffId") String staffId,
			List<EnumStaffFunction> staffFunctionList) throws Exception {
		userBusiness.updateStaffRoles(Integer.valueOf(staffId), staffFunctionList);
		return Response.ok("Roles of teacher have been changed successfully").build();
	}

	@POST
	@Path("/staffs/{staffId}/attendances")
	@RolesAllowed("staff")
	@Produces(MediaType.APPLICATION_JSON)
	public Response createStaffAttendance(@PathParam("staffId") String staffId,
			StaffAttendanceDto staffAttendanceDto) throws Exception {
		if (staffAttendanceDto.getStaff() == null) {
			staffAttendanceDto.setStaff(new StaffDto());
		}
		staffAttendanceDto.getStaff().setStaffId(Integer.valueOf(staffId));
		staffAttendanceDto = userBusiness.saveStaffAttendance(staffAttendanceDto);
		return Response.status(Response.Status.CREATED).entity(staffAttendanceDto).build();
	}

	@PUT
	@Path("/staffs/attendances/{attendanceId}")
	@RolesAllowed("staff")
	public Response updateStaffAttendance(
			@PathParam("attendanceId") String attendanceId,
			StaffAttendanceDto staffAttendanceDto) throws Exception {
		staffAttendanceDto.setStaffAttendanceId(Integer.valueOf(attendanceId));
		if (staffAttendanceDto.getStaff() == null) {
			staffAttendanceDto.setStaff(new StaffDto());
		}
		staffAttendanceDto = userBusiness.saveStaffAttendance(staffAttendanceDto);
		return Response.ok("Staff attendance updated").build();
	}

	@DELETE
	@Path("/staffs/attendances/{attendanceId}")
	@RolesAllowed("staff")
	public Response deleteStaffAttendance(@PathParam("attendanceId") String attendanceId) throws Exception {
		userBusiness.deleteStaffAttendance(Integer.valueOf(attendanceId));
		return Response.ok("Staff attendance deleted").build();
	}

	@GET
	@RolesAllowed("user")
	@Path("/staffPerformanceReport/{staffId}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getStaffPerformanceReport(@PathParam("staffId") String staffId) throws Exception {
		return Response.status(Response.Status.OK).entity(
				userBusiness.getStaffReportPerformance(Integer.valueOf(staffId))).build();
	}

	@GET
	@RolesAllowed("user")
	@Path("/studentPerformanceReport/{studentId}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getStudentPerformanceReport(@PathParam("studentId") String studentId) throws Exception {
		return Response.status(Response.Status.OK).entity(
				userBusiness.getStudentReportPerformance(Integer.valueOf(studentId))).build();
	}
}
